import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Solution870 {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        List<Integer> list= new ArrayList<>();
        for (int i : nums1) {
            list.add(i);
        }
        list.sort(Comparator.comparingInt(a -> a));
        int[] ans=new int[nums1.length];
        for (int i=0;i<nums2.length;i++){
            int l=0,r=list.size()-1,t=-1;
            while (l<=r){
                int mid=(l+r)/2;
                if (list.get(mid)>nums2[i]){
                    t=mid;
                    r=mid-1;
                }else {
                    l=mid+1;
                }
            }
            if (t==-1){
                ans[i]=list.remove(0);
            }else{
                ans[i]=list.remove(t);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution870().advantageCount(new int[]{2, 7, 11, 15}, new int[]{1, 10, 4, 11})));
    }
}
